﻿using MgSoft.K3Cloud.WebApi.Common.Dto.Bd;
using MgSoft.K3Cloud.WebApi.Dto;
using System;
using System.Collections.Generic;
using System.Text;

namespace MgSoft.K3Cloud.WebApi.Common.Api.Bd
{
    /// <summary>
    /// 科目
    /// </summary>
    public class Bd_AccountApi : BaseApi, IBd_AccountApi
    {
        public List<SubjectVerificationScope> subjectVerificationScopes { get; set; }

        protected override string formId => "Bd_Account";
        public Bd_AccountApi(ApiServerInfo apiServerInfo, IMgLogger logger = null) : base(apiServerInfo, logger)
        {
            subjectVerificationScopes = new List<SubjectVerificationScope>();
        }

        public Bd_AccountApi(string serverUrl, string dbid, string userName, string password, int lcid = 2052) : base(serverUrl, dbid, userName, password, lcid)
        {
        }

        /// <summary>
        /// 建议使用这个查询。查询后会列出核算维度内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="getInputDto"></param>
        /// <returns></returns>
        public T GetAccount<T>(GetInputDto getInputDto) where T : Bd_AccountGetOutputDto
        {
            var result = base.Get<T>(getInputDto);

            setSubjectVerificationScopes(result);

            return result;
        }


        /// <summary>
        /// 构建核算维度对应内容
        /// </summary>
        private void setSubjectVerificationScopes(Bd_AccountGetOutputDto bd_AccountGetOutputDto)
        {
            foreach (var flexEntryRow in bd_AccountGetOutputDto.BD_ACCOUNTFLEXENTRY)
            {
                var subjectVerificationScope = new SubjectVerificationScope();

                subjectVerificationScope.Account = bd_AccountGetOutputDto;
                subjectVerificationScope.Id = flexEntryRow.FlEXITEMPROPERTYID_Id;
                subjectVerificationScope.Number = flexEntryRow.FlEXITEMPROPERTYID.Number;
                subjectVerificationScope.BaseName = flexEntryRow.FlEXITEMPROPERTYID.Name[0].Value;
                subjectVerificationScope.InputType = flexEntryRow.InputType;
                subjectVerificationScope.FlexNumber = flexEntryRow.FlEXITEMPROPERTYID.FlexNumber;

                this.subjectVerificationScopes.Add(subjectVerificationScope);
            }
        }
    }

    /// <summary>
    /// 科目核算维度
    /// </summary>
    public class SubjectVerificationScope
    {
        /// <summary>
        /// 核算维度Id
        /// </summary>
        public virtual int Id { get; set; }
        /// <summary>
        /// 核算维度名称
        /// </summary>
        public virtual string Name { get; set; }
        /// <summary>
        /// 核算维度编码
        /// </summary>
        public virtual string Number { get; set; }
        /// <summary>
        /// 是否必录(1：必录，0：非必录)
        /// </summary>
        public virtual string InputType { get; set; }
        /// <summary>
        /// 所属科目
        /// </summary>
        public virtual Bd_AccountGetOutputDto Account { get; set; }
        /// <summary>
        /// 核算维度基础资料名称
        /// </summary>
        public virtual string BaseName { get; set; }
        /// <summary>
        /// 核算维度对应的基础资料类型
        /// </summary>
        public virtual string Type { get; set; }
        /// <summary>
        /// 金蝶核算维度列名；
        /// 在对接凭证保存Api时，需要根据这个列名，来对应具体的核算维度
        /// </summary>
        public virtual string FlexNumber { get; set; }
        /// <summary>
        /// 核算维度值
        /// </summary>
        public virtual string Value { get; set; }
    }
}
